package com.fengchao.api.controller.v1;

import com.alibaba.fastjson.JSON;
import com.fengchao.api.dao.cfg.CfgChinaCityListMapper;
import com.fengchao.api.service.CfgService;
import com.fengchao.api.service.PfmService;
import com.fengchao.api.service.UsrService;
import com.fengchao.model.cfg.CfgChinaCityList;
import com.fengchao.model.cfg.CfgRegion;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.entity.comm.Message;
import com.fengchao.model.entity.comm.ResponseParameter;
import com.fengchao.util.common.CommonValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fengchao.api.config.Constant.*;

/**
 * Created by http://my.oschina.net/wangnian on 2016/9/8.
 */
@RequestMapping("/v1/pfm")
@RestController
public class PfmController extends ExceptionController {

    @Autowired
    UsrService usrService;
    @Autowired
    PfmService pfmService;
    @Autowired
    CfgService cfgService;
    @Autowired
    CfgChinaCityListMapper cfgChinaCityListMapper;

    private static Logger logger = LoggerFactory.getLogger(OdrController.class);

    /**
     * 演出列表
     *
     * @return
     */
    @ApiOperation(value = "(APP)获取演出列表", notes = "返回参数: </br>成功: </br>{status: SUCCESS, </br>message: 获取成功, </br>data: {</br>&nbsp;&nbsp;info: [</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;musicStyle: POP(音乐风格),</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmTiTle: aaa(演出标题)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmTime: 1473657272000(演出时间),</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmId: 1(演出ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;shareUrl: http://aaa.com(演出分享链接),</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmStatus: PROCESS(状态(EDIT/PROCESS/CANCEL)),</br>&nbsp;&nbsp;&nbsp;&nbsp;isFavorite: 1(是否收藏 1:是 0:否)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmImageUrl: http://aaa.png (演出图片地址),</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmTimeRange: aaa(演出时间范围)</br>&nbsp;&nbsp;&nbsp;&nbsp;fieldName: aaa(场地名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;priceRange: 100-150(价格范围)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败: </br>{status: FAILURE, message: 参数不全}</br>{status: FAILURE, message: 获取失败}")
    @RequestMapping(value = "/getPerformanceList/{cityCode}/{districtCode}/{fieldType}/{nearTime}/{musicStyle}/{page}/{count}", method = RequestMethod.GET)
    public ResponseParameter getPerformanceList(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type,
                                                @ApiParam(value = "用户token") @RequestHeader(required = false) String token,
                                                @ApiParam(value = "(必填)城市Code", required = true) @PathVariable("cityCode") String cityCode,
                                                @ApiParam(value = "(必填)区Code", required = true) @PathVariable("districtCode") String districtCode,
                                                @ApiParam(value = "(必填)场地类型, BAR/LIVEHOUSE/THEATRE/STADIUM/</br>CONVERTION_CENTER/CONCERT_HALL/ALL", required = true) @PathVariable("fieldType") String fieldType,
                                                @ApiParam(value = "(必填)最近的时间 ONEWEEK 近一周/ONEMONTH 近一个月/</br>THREEMONTHS 近三个月/HALFYEAR 近半年/ALL", required = true) @PathVariable("nearTime") String nearTime,
                                                @ApiParam(value = "(必填)音乐风格,POP 流行/ROCK：摇滚/FOLK：民谣/R&B/REGGAE</br>  雷鬼/ELECTRONIC：电子/HIP-HOP：嘻哈/ALL", required = true) @PathVariable("musicStyle") String musicStyle,
                                                @ApiParam(value = "(必填)第几页", required = true) @PathVariable("page") Integer page,
                                                @ApiParam(value = "(必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取演出列表接口请求参数 type:{} ,token:{},cityCode:{},districtCode:{},fieldType:{},nearTime:{},musicStyle:{},page:{},count:{}", type, token, cityCode, districtCode, fieldType, nearTime, musicStyle, page, count);
        Integer userId = usrService.getUserId(type, token, false);
        CfgChinaCityList cfgChinaCityList = cfgChinaCityListMapper.findByCityCode(cityCode);
        Integer cityId = cfgChinaCityList == null ? 0 : cfgChinaCityList.getCityId();
        List<Map<String, Object>> resultList = pfmService.getPerformanceList(userId, cityId, fieldType, musicStyle, nearTime, page, count);
        Map resultMap = new HashMap();
        resultMap.put("info", resultList);
        responseParameter.setData(resultMap);
        logger.debug("获取演出列表接口响应参数 :{}", JSON.toJSON(responseParameter));
        logger.info("获取演出列表接口响应参数 条数:{}", resultList.size());
        return responseParameter;
    }


    /**
     * 演出详情
     *
     * @param type
     * @param token
     * @param performanceId
     * @return
     */
    @ApiOperation(value = "(APP/WEB)获取某个的演出详情", notes = "返回参数: </br>成功: </br>{status: SUCCESS, </br>message: 获取成功, </br>data: {</br>&nbsp;info: {</br>&nbsp;&nbsp;&nbsp;pfmTitle: aaa(演出标题),</br>&nbsp;&nbsp;&nbsp;pfmTime: 1473657272000(演出开始时间),</br>&nbsp;&nbsp;&nbsp;pfmImageUrl: http://aaa.png (演出图片地址),</br>&nbsp;&nbsp;&nbsp;pfmTimeRange: aaa(演出时间范围)," +
            "</br>&nbsp;&nbsp;&nbsp;fieldName: aaa(场地名称),</br>&nbsp;&nbsp;&nbsp;fieldAdress: aaa(场地地址),</br>&nbsp;&nbsp;&nbsp;longitude: 11.00(场地经度坐标),</br>&nbsp;&nbsp;&nbsp;latitude: 11.00(场地纬度坐标), </br>&nbsp;&nbsp;&nbsp;pfmInfoUrl: http://aa.com (演出详情),</br>&nbsp;&nbsp;&nbsp;buyNoticeUrl: http://aaa.com (购买须知),</br>&nbsp;&nbsp;&nbsp;shareUrl: http://aaa.com (分享的链接)," +
            "</br>&nbsp;&nbsp;&nbsp;isFavorite: 0(是否收藏 1: 是 0: 否), </br>&nbsp;&nbsp;&nbsp;priceRange: 100-150(票价范围),</br>&nbsp;&nbsp;&nbsp;artistList: [</br>&nbsp;&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID), </br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;artistDisplay: aaa(艺人展示名),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nationality: aaa(艺人国籍), " +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;headIconApp: http://aaa.png (APP端艺人头像)</br>&nbsp;&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;&nbsp;]</br>&nbsp;&nbsp;}</br>&nbsp;}</br>}")
    @RequestMapping(value = "/getPerformanceDetail/{performanceId}", method = RequestMethod.GET)
    public ResponseParameter getPerformanceDetail(@ApiParam(value = "(APP/WEB必填)类型,APP/WEB", required = true) @RequestHeader String type,
                                                  @ApiParam(value = "用户token") @RequestHeader(required = false) String token,
                                                  @ApiParam(value = "(APP/WEB必填)", required = true) @PathVariable("performanceId") Integer performanceId) {
        ResponseParameter responseParameter = new ResponseParameter(Message.FAILURE, GET_FAILURE);
        logger.info("获取某个的演出详情 type:{} ,token:{},performanceId:{}", type, token, performanceId);
        Integer userId = usrService.getUserId(type, token, false);
        Map<String, Object> resultMap = pfmService.getPfmInfo(performanceId, userId);
        if (resultMap != null) {
            responseParameter.setStatus(SUCCESS);
            responseParameter.setMessage(GET_SUCCESS);
            Map<Object, Object> map = new HashMap<>();
            map.put("info", resultMap);
            responseParameter.setData(map);
        }
        logger.error("获取某个的演出详情响应参数 :{}", JSON.toJSON(responseParameter));
        logger.info("获取某个的演出详情响应参数 :{}", responseParameter.getStatus());
        return responseParameter;
    }

    /**
     * 演出详情
     *
     * @param type
     * @param performanceId
     * @return
     */
    @ApiOperation(value = "(APP/WEB)获取演唱会的信息和须知", notes = "返回参数: </br>成功:")
    @RequestMapping(value = "/getpfmInfoBuyNotice/{performanceId}/{category}", method = RequestMethod.GET)
    public ResponseParameter getpfmInfoBuyNotice(@ApiParam(value = "(APP/WEB)类型, APP/WEB", required = true) @RequestHeader String type,
                                                 @ApiParam(value = "(APP/WEB)演出ID", required = true) @PathVariable("performanceId") Integer performanceId,
                                                 @ApiParam(value = "(APP/WEB)INFO/NOTICE") @PathVariable("category") String category) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取演唱会的信息和须知请求参数: type:{} ,performanceId:{},category:{}", type, performanceId, category);
        String info = pfmService.getpfmInfoBuyNotice(performanceId, category);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", info);
        responseParameter.setData(map);
        logger.debug("获取演唱会的信息和须知响应参数: :{}", JSON.toJSON(responseParameter));
        logger.info("获取演唱会的信息和须知响应参数: :{}", responseParameter.getStatus());
        return responseParameter;
    }


    /**
     * 获取演出票的的购买项
     *
     * @param type
     * @param token
     * @param performanceId
     * @return
     */
    @ApiOperation(value = "(APP)获取演出票的的购买项", notes = "返回参数: </br>成功: </br>{status: SUCCESS, </br>message: 获取成功, </br>data: {</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmId: 1(演出ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;pfmSingleName: aaa(演出场次名字),</br>&nbsp;&nbsp;&nbsp;&nbsp;memo: (场次简介)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;ticketList:[</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ticketInfo: aaa(门票简介), </br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ticketPrice: 123(门票价格),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memo： aaa(门票的简介)</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}" +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;]</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败: </br>{status: FAILURE, message: 获取失败}")
    @RequestMapping(value = "/getTicketPrice/{performanceId}", method = RequestMethod.GET)
    public ResponseParameter getTicketPrice(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type,
                                            @ApiParam(value = "(必填)用户token", required = true) @RequestHeader String token,
                                            @ApiParam(value = "(必填)演出ID", required = true) @PathVariable("performanceId") Integer performanceId) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取演出票的的购买项请求参数: type:{} ,token:{},performanceId:{}", type, token, performanceId);
        // Integer userId = usrService.getUserId(type, token, false);
        List<Map<String, Object>> resultList = pfmService.getPurchaseItems(performanceId);
        Map resultMap = new HashMap();
        resultMap.put("info", resultList);
        responseParameter.setData(resultMap);
        logger.debug("获取演出票的的购买项响应参数:{}", JSON.toJSON(responseParameter));
        logger.info("获取演出票的的购买项响应参数:{}", responseParameter.getStatus());
        return responseParameter;
    }

    /**
     * 获取城市/热门城市
     *
     * @param type
     * @return
     */
    @ApiOperation(value = "(APP)获取城市/热门城市接口", notes = "返回参数: </br>成功: </br>{status: SUCCESS, </br>message: 获取成功, </br>data: {</br>&nbsp;&nbsp;info:{</br>&nbsp;&nbsp;&nbsp;cityList:[</br>&nbsp;&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letter: B(城市首字母),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letterList:[" +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: 2(城市ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pid: 2(城市的父ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;code: 110100(code),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: 北京市(城市名)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level: 3(级别，2：省（直辖市），3：市，4：区),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cityCode: aaa(城市code),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oderId: 1(排序)</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]" +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;&nbsp;],</br>&nbsp;&nbsp;&nbsp;popList:[</br>&nbsp;&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: 1(城市ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pid: 1(城市的父ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;code: 110100(code),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: 北京市(城市名)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level: 3(级别，2：省（直辖市），3：市，4：区),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cityCode: (城市code),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oderId: 1(排序)</br>&nbsp;&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;&nbsp;]</br>&nbsp;&nbsp;}</br>&nbsp;}</br>}" +
            "</br>失败: </br>{status: FAILURE, message: 获取失败}")
    @RequestMapping(value = "/getPerformaceCity", method = RequestMethod.GET)
    public ResponseParameter getPerformaceCity(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        // Integer userId = usrService.getUserId(type, token, false);
        logger.info("获取城市/热门城市接口请求参数: type:{}", type);
        Map resultMap = new HashMap();
        resultMap.put("info", pfmService.getPfmCity());
        responseParameter.setData(resultMap);
        logger.info("获取城市/热门城市响应参数:{}", responseParameter.getStatus());
        return responseParameter;
    }

    @RequestMapping(value = "/getFavoritePerformace/{page}/{count}", method = RequestMethod.GET)
    @ApiOperation(value = "(APP)获取收藏的演出", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;appImage: http://aaa.png (APP端演出的图片),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 1(演出的ID)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;title: aaa(演出的标题)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}")
    public ResponseParameter getFavoritePerformace(@ApiParam(value = "(必填)类型,APP", required = true) @RequestHeader(required = false) String type,
                                                   @ApiParam(value = "(必填)用户token", required = true) @RequestHeader String token,
                                                   @ApiParam(value = "(必填)第几页", required = true) @PathVariable("page") Integer page,
                                                   @ApiParam(value = "(必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取收藏的演出请求参数= type：{},token:{},page:{},count:{}", type, token, page, count);
        if (!CommonValidator.isStringsNotNull(type, token, page, count)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        Integer userId = usrService.getUserId(type, token, true);
        List<Map> list = pfmService.getFavoritePerformace(userId, page, count);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.debug("获取收藏的演出响应参数={}", JSON.toJSON(responseParameter));
        logger.info("获取收藏的演出响应参数= 获取的数量{}", list.size());
        return responseParameter;
    }


}
